<template>
  <div class="app-container">
    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="类型名称" prop="typeName">
          <el-input v-model="queryParams.typeName" placeholder="请输入类型名称" clearable />
        </el-form-item>
        <el-form-item label="类型编码" prop="typeCode">
          <el-input v-model="queryParams.typeCode" placeholder="请输入类型编码" clearable />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
            <el-option label="正常" :value="1" />
            <el-option label="停用" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card shadow="never" class="table-wrapper">
      <template #header>
        <el-button type="primary" @click="openForm()">
          <el-icon><Plus /></el-icon>
          新增
        </el-button>
      </template>

      <el-table v-loading="loading" :data="list">
        <el-table-column type="index" label="序号" width="50" />
        <el-table-column prop="typeName" label="类型名称" />
        <el-table-column prop="typeCode" label="类型编码" />
        <el-table-column prop="description" label="描述" show-overflow-tooltip />
        <el-table-column prop="status" label="状态" width="80">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'danger'">
              {{ row.status === 1 ? '正常' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="180" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="openForm(row.id)">
              <el-icon><Edit /></el-icon>
              修改
            </el-button>
            <el-button type="danger" link @click="handleDelete(row.id)">
              <el-icon><Delete /></el-icon>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination 
        v-if="total > 0" 
        v-model:page="queryParams.pageNum" 
        v-model:limit="queryParams.pageSize" 
        :total="total" 
        @pagination="getList" 
      />
    </el-card>

    <!-- 表单弹窗 -->
    <el-dialog v-model="dialogVisible" :title="form.id ? '修改机器类型' : '新增机器类型'" width="500px" append-to-body destroy-on-close>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="类型名称" prop="typeName">
          <el-input v-model="form.typeName" placeholder="请输入类型名称" />
        </el-form-item>
        <el-form-item label="类型编码" prop="typeCode">
          <el-input v-model="form.typeCode" placeholder="请输入类型编码" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="dialogVisible = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Search, Refresh, Plus, Edit, Delete } from '@element-plus/icons-vue';
import { listMachineType, getMachineType, addMachineType, updateMachineType, delMachineType } from '@/api/vending/machineType';
import type { MachineTypeQuery, MachineTypeVO, MachineTypeForm } from '@/api/vending/machineType/types';
import { debounce } from 'lodash-es';

const loading = ref(false);
const list = ref<MachineTypeVO[]>([]);
const total = ref(0);
const dialogVisible = ref(false);
const queryFormRef = ref();
const formRef = ref();

const queryParams = reactive<MachineTypeQuery>({
  pageNum: 1,
  pageSize: 10,
  typeName: '',
  typeCode: '',
  status: undefined
});

const form = reactive<MachineTypeForm>({
  typeName: '',
  typeCode: '',
  description: '',
  status: 1
});

const rules = {
  typeName: [{ required: true, message: '请输入类型名称', trigger: 'blur' }],
  typeCode: [{ required: true, message: '请输入类型编码', trigger: 'blur' }],
  status: [{ required: true, message: '请选择状态', trigger: 'change' }]
};

const getList = async () => {
  loading.value = true;
  try {
    const res = await listMachineType(queryParams);
    list.value = res.rows;
    total.value = res.total;
  } finally {
    loading.value = false;
  }
};

const debouncedGetList = debounce(() => {
  getList();
}, 300);

const handleQuery = () => {
  queryParams.pageNum = 1;
  debouncedGetList();
};

const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

const openForm = async (id?: number) => {
  dialogVisible.value = true;
  Object.assign(form, {
    id: undefined,
    typeName: '',
    typeCode: '',
    description: '',
    status: 1
  });
  
  if (id) {
    const res = await getMachineType(id);
    Object.assign(form, res.data);
  }
};

const submitForm = async () => {
  await formRef.value?.validate();
  if (form.id) {
    await updateMachineType(form);
    ElMessage.success('修改成功');
  } else {
    await addMachineType(form);
    ElMessage.success('新增成功');
  }
  dialogVisible.value = false;
  getList();
};

const handleDelete = async (id: number) => {
  await ElMessageBox.confirm('确认要删除该机器类型吗？', '提示', {
    type: 'warning'
  });
  await delMachineType([id]);
  ElMessage.success('删除成功');
  getList();
};

onMounted(() => {
  getList();
});
</script>

<style scoped>
.search-wrapper {
  margin-bottom: 20px;
  border-radius: 8px;
}

.table-wrapper {
  border-radius: 8px;
}

:deep(.el-card__header) {
  padding: 12px 20px;
  border-bottom: 1px solid #ebeef5;
}

:deep(.el-card__body) {
  padding: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}
</style> 